import os
import subprocess



# $ DATASET_PATH=/path/to/dataset

# $ colmap feature_extractor \
#    --database_path $DATASET_PATH/database.db \
#    --image_path $DATASET_PATH/images

# $ colmap exhaustive_matcher \
#    --database_path $DATASET_PATH/database.db

# $ mkdir $DATASET_PATH/sparse

# $ colmap mapper \
#     --database_path $DATASET_PATH/database.db \
#     --image_path $DATASET_PATH/images \
#     --output_path $DATASET_PATH/sparse

# $ mkdir $DATASET_PATH/dense
#跑colmap
def run_colmap(basedir, match_type):
    
    logfile_name = os.path.join(basedir, 'colmap_output.txt')#日志文件保存路径
    logfile = open(logfile_name, 'w')#打开日志文件
    
    # feature_extractor_args = [#特征匹配参数
    #     'colmap', 'feature_extractor', 
    #         '--database_path', os.path.join(basedir, 'database.db'), 
    #         '--image_path', os.path.join(basedir, 'images'),
    #         '--ImageReader.single_camera', '1',#假设拍摄的图片使用相同的相机内参和畸变参数
    #         # '--SiftExtraction.use_gpu', '0',
    # ]
    # #记录特征提取日志信息
    # feat_output = ( subprocess.check_output(feature_extractor_args, universal_newlines=True) )
    # logfile.write(feat_output)
    # print('Features extracted')
    # #特征匹配
    # exhaustive_matcher_args = [
    #     'colmap', match_type, 
    #         '--database_path', os.path.join(basedir, 'database.db'), 
    # ]

    # match_output = ( subprocess.check_output(exhaustive_matcher_args, universal_newlines=True) )
    # logfile.write(match_output)
    # print('Features matched')
    
    # p = os.path.join(basedir, 'sparse')
    # if not os.path.exists(p):
    #     os.makedirs(p)

    # #保存点云三维重建结果
    # mapper_args = [
    #     'colmap', 'mapper',
    #         '--database_path', os.path.join(basedir, 'database.db'),
    #         '--image_path', os.path.join(basedir, 'images'),
    #         '--output_path', os.path.join(basedir, 'sparse'), # --export_path changed to --output_path in colmap 3.6
    #         '--Mapper.num_threads', '16',
    #         '--Mapper.init_min_tri_angle', '4',
    #         '--Mapper.multiple_models', '0',
    #         '--Mapper.extract_colors', '0',
    # ]
    # #
    # map_output = ( subprocess.check_output(mapper_args, universal_newlines=True) )
    # logfile.write(map_output)
    
    # print('Sparse map created')
    # #转化为ply格式
    # p = os.path.join(basedir, 'dense')
    # if not os.path.exists(p):
    #     os.makedirs(p)
    
    # image_undistorter_args = [
    #     'colmap', 'image_undistorter',
    #         '--image_path', os.path.join(basedir, 'images'),
    #         '--input_path', os.path.join(basedir, 'sparse/0'), 
    #         '--output_path', os.path.join(basedir, 'dense'), 
    #         '--output_type', 'COLMAP',
    #         '--max_image_size', '2000',
    # ]
    # image_undistorter_output = ( subprocess.check_output(image_undistorter_args, universal_newlines=True) )
    # logfile.write(image_undistorter_output)

    # patch_match_stereo_args = [
    #     'colmap', 'patch_match_stereo',
    #         '--workspace_path', os.path.join(basedir, 'dense'),
    #         '--workspace_format', 'COLMAP', 
    #         '--PatchMatchStereo.geom_consistency', 'true', 
    # ]
    # patch_match_stereo_output = ( subprocess.check_output(patch_match_stereo_args, universal_newlines=True) )
    # logfile.write(patch_match_stereo_output)

    # stereo_fusion_args = [
    #     'colmap', 'stereo_fusion',
    #         '--workspace_path', os.path.join(basedir, 'dense'),
    #         '--workspace_format', 'COLMAP', 
    #         '--input_type', 'geometric', 
    #         '--output_path', os.path.join(basedir, 'fused.ply'), 
    # ]
    # stereo_fusion_output = ( subprocess.check_output(stereo_fusion_args, universal_newlines=True) )
    # logfile.write(stereo_fusion_output)

    #一步到位
    automatic_reconstructor_args = [
        'colmap', 'automatic_reconstructor',
        '--workspace_path', basedir, 
        '--image_path', os.path.join(basedir,"images"), 
            '--sparse', "1", 
            '--dense', "1", 
    ]
    automatic_reconstructor_output = ( subprocess.check_output(automatic_reconstructor_args, universal_newlines=True) )
    logfile.write(automatic_reconstructor_output)

    convert_args = [
        'colmap', 'model_converter',
            '--input_path', os.path.join(basedir, 'dense/0/sparse'), 
            '--output_path', os.path.join(basedir, 'out.ply'), 
            '--output_type', 'PLY',
    ]
    convert_output = ( subprocess.check_output(convert_args, universal_newlines=True) )
    logfile.write(convert_output)




    logfile.close()
    print( 'Finished running COLMAP, see {} for logs'.format(logfile_name) )


